#论坛系统用登录页自动化测试
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from ForumAutoTest.Common.Utils import ForumDriver


class ForumLogin:
    # 通过url访问指定页面
    url = ""
    driver = ""

    def __init__(self):
        # 用户登入界面网址
        self.url = "http://127.0.0.1:58080/sign-in.html"
        # 调用配置的浏览器Driver,再通过driver.get访问论坛系统用户登录页面
        self.driver = ForumDriver.driver
        # 打开指定的url
        self.driver.get(self.url)
        sleep(1)
    # 成功登录
    def LoginSuccess(self):
        #跳转到论坛登录首页
        self.driver.get(self.url)

        #定位用户登录页元素
        self.driver.find_element(By.CSS_SELECTOR, "body > div.page.page-center > div > div > div:nth-child(1) > div > div.card.card-md > div > h2")
        #确保用户与密码框为空
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        # 定位到用户名输入框，输入正确的账号
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("666")
        sleep(1)
        # 定位到用户密码输入框，输入正确的密码
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("666")
        sleep(1)
        # 定位到登录按钮并且点击
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        sleep(1)
        # 判断是否登录成功,成功即可定位到以下元素，并且截图查看
        #定位论坛板块导航
        self.driver.find_element(By.CSS_SELECTOR, "body > div.page > header:nth-child(2)")
        #定位正文部分
        self.driver.find_element(By.CSS_SELECTOR, "#page-wrapper")

        # 添加屏幕截图--1
        ForumDriver.getScreeShot()
        sleep(1)

    # 未成功登录
    def LoginFail(self):
        self.driver.get(self.url)
        # 重新进入论坛系统用户登入界面，会出现关键词拼接，所以用clear()清理账号与密码框
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        sleep(1)
        #不输入账户
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        sleep(1)
        # 定位到提示并返回文本
        case1 = self.driver.find_element(By.CSS_SELECTOR, "#signInForm > div.mb-3 > div").text
        case2 = self.driver.find_element(By.CSS_SELECTOR, "#signInForm > div.mb-2 > div > div").text
        print(case1)
        print(case2)
        assert case1 == "用户名不能为空"
        assert case2 == "密码不能为空"
        sleep(1)
        # 添加屏幕截图--1
        ForumDriver.getScreeShot()



        # 输入错误账户，不输入密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("55555")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        sleep(1)
        # 定位到提示并返回文本
        case1 = self.driver.find_element(By.CSS_SELECTOR, "#signInForm > div.mb-2 > div > div").text
        print(case1)
        assert case1 == "密码不能为空"
        sleep(1)
        # 添加屏幕截图--2
        ForumDriver.getScreeShot()



        # 不输入账户，输入密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("666")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        sleep(1)
        # 定位到提示并返回文本
        case1 = self.driver.find_element(By.CSS_SELECTOR, "#signInForm > div.mb-3 > div").text
        print(case1)
        assert case1 == "用户名不能为空"
        sleep(1)
        # 添加屏幕截图--3
        ForumDriver.getScreeShot()



        # 输入错误的账户与，错误的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("55555")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("666666")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        sleep(1)
        wait = WebDriverWait(self.driver, 2)
        wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div")))
        warning_message = self.driver.find_element(By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div").text
        print(warning_message)
        assert "用户名或密码错误" in warning_message
        sleep(1)
        # 添加屏幕截图--4
        ForumDriver.getScreeShot()



        #输入正确的账户，错误的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("666")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("6666666")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        sleep(1)
        wait = WebDriverWait(self.driver, 2)
        wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div")))
        warning_message = self.driver.find_element(By.CSS_SELECTOR, "body > div.jq-toast-wrap.bottom-right > div").text
        print(warning_message)
        assert "用户名或密码错误" in warning_message
        sleep(1)
        # 添加屏幕截图--5
        ForumDriver.getScreeShot()



        #点击注册
        self.driver.find_element(By.CSS_SELECTOR, "body > div.page.page-center > div > div "
                                                  "> div:nth-child(1) > div > div.text-center.text-muted.mt-3 > a").click()
        sleep(1)
        # 添加屏幕截图--6
        ForumDriver.getScreeShot()
